﻿namespace BEGDC.CC.Web.UserControls.CV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using BEGDC.CC.BL.CCFlow.CV;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.CC.BL.Common.Query;
	using BEGDC.Utility.Castle;
	using BEGDC.CC.ExcelTool;

	/// <summary>
	///		Summary description for Assignment.
	/// </summary>
	public class Assignment : BaseCV
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.ImageButton btnSubmit;
		protected System.Web.UI.WebControls.ImageButton btnQuery;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected System.Web.UI.WebControls.ImageButton btnCheck;
		protected System.Web.UI.WebControls.ImageButton btnReject;
		protected System.Web.UI.WebControls.ImageButton btnReply;
		protected System.Web.UI.WebControls.ImageButton btnSave;
		protected Apply uclCV;

		private bool IsQuery
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				return state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}
		protected string Step
		{
			get
			{
				IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
				FlowInstance flowInstance = queryService.QueryFlowInstance( this.AppID );
				if( flowInstance == null )
					return "Close";
				else
					return flowInstance.CurrentActiveStep.StepCode;
			}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!Page.IsPostBack)
			{
//				SetStamp();
				this.btnQuery.Attributes.Add("onclick", "javascript:OpenQueryPane("+Request.QueryString["AppID"]+");");
				this.btnReject.Attributes.Add("onclick", "javascript:OpenCommentPane();");
				this.btnReply.Attributes.Add("onclick", "javascript:OpenReplyPane("+Request.QueryString["FlowID"]+");");
				this.btnSubmit.Attributes.Add("onclick", "javascript:OpenAssignPane("+Request.QueryString["AppID"]+");");
				this.btnCheck.Attributes.Add("onclick", "javascript:OnCheck();");

//				Accessibility access = this.CheckAccessibility( HttpContext.Current.User.Identity.Name, BEGDC.CC.BL.Workflow.UserRole.CODMgr );
//				ControlStatusSetting( access );
				
				uclCV.DisableCATBranch();

//				if(this.IsQuery)
//				{
//					
//					this.btnReject.Visible = false;
//					this.btnQuery.Visible = false;
//					this.btnCheck.Visible = false;
//					this.btnSubmit.ImageUrl = "../../images/bbw_reply.gif";
//				}
//				else
//				{
//					
//					this.btnSubmit.Attributes.Add("check", "true");
//				}
				
//				if( this.Step != BEGDC.CC.BL.Workflow.WorkTransition.CV_Assignment && !this.IsQuery)
//				{
//					this.Label1.Text = string.Empty;
//					this.btnQuery.Visible = false;
//					this.btnSubmit.Visible = false;
//					this.btnCheck.Visible = false;
//					this.btnReject.Visible = false;
//				}
			}
			ButtonStatusSetting();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnQuery.Click += new System.Web.UI.ImageClickEventHandler(this.btnQuery_Click);
			this.btnReply.Click += new System.Web.UI.ImageClickEventHandler(this.btnReply_Click);
			this.btnSubmit.Click += new System.Web.UI.ImageClickEventHandler(this.btnSubmit_Click);
			this.btnCheck.Click += new System.Web.UI.ImageClickEventHandler(this.btnCheck_Click);
			this.btnReject.Click += new System.Web.UI.ImageClickEventHandler(this.btnReject_Click);
			this.btnSave.Click += new System.Web.UI.ImageClickEventHandler(this.btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void btnSubmit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				string msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID );
				if( msg + string.Empty != string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM003 + "');</script>");
					return;
				}
				ICV cvService = AppContainer.Instance[ServiceConst.CVService] as ICV;
				if(this.tbOperationFlag.Text == "assignment")
				{
					this.uclCV.Save(false);
					cvService.Assign(Convert.ToInt64(Request.QueryString["FlowID"]), CVTransitionList.Assign, this.tbOperationContent.Text, 
						Convert.ToInt32(Request.QueryString["AppID"]), string.Empty);
				}
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnQuery_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				if(this.tbOperationFlag.Text == "query")
				{
					ICV cvService = AppContainer.Instance[ServiceConst.CVService] as ICV;
					cvService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());
					Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
				}
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void ControlStatusSetting(Accessibility access )
		{
			if( access == Accessibility.Readonly )
			{
				this.DisableChildrenControls(this);
			}
		}

		private void btnCheck_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				string msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID );
				if( msg + string.Empty != string.Empty)
				{
					Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM003 + "')</script>");
					return;
				}

				ICV cvService = AppContainer.Instance[ServiceConst.CVService] as ICV;
				this.uclCV.Save(false);
				cvService.Check(Convert.ToInt64(Request.QueryString["FlowID"]), CVTransitionList.Check, WebUtility.DeCodeString(Server, tbComment.Text));
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnReject_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ICV cvService = AppContainer.Instance[ServiceConst.CVService] as ICV;
				
				cvService.Assign(Convert.ToInt64(Request.QueryString["FlowID"]), CVTransitionList.Reject, this.tbOperationContent.Text, 
						Convert.ToInt32(Request.QueryString["AppID"]), WebUtility.DeCodeString(Server, tbComment.Text));
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void Btn_Export_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ExcelTool.ICCExcelExport excelExport=new ExcelTool.ExportCVForm();
				excelExport.Export(this.Page, this.AppID );
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
			}
			catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}

		}

		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.btnSubmit.Visible = this.Access.IsButtonVisible(CVTransitionList.Assign);
			this.btnCheck.Visible = this.Access.IsButtonVisible(CVTransitionList.Check);
			this.btnReject.Visible = this.Access.IsButtonVisible(CVTransitionList.Reject);
			this.btnReply.Visible = this.Access.IsButtonVisible(CVTransitionList.Reply) && this.Step == WorkTransition.CV_Assignment;
		}

		private void btnReply_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ICV cvService = AppContainer.Instance[ServiceConst.CVService] as ICV;
				if(this.tbOperationFlag.Text == "reply")
				{
					this.uclCV.Save(false);
					cvService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));
				}
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				this.uclCV.Save(false);
				string url = string.Empty + Request.Form["tbURL"];
				string script = "<script>";
				if(url == string.Empty)
					script += "VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');";
				if(url != string.Empty)
				{
					script += "window.location.href='"+url+"'";
				}
				script += "</script>";

				Page.RegisterClientScriptBlock("",script);
//				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}
	}
}
